package com.hainatong.jcmvvmbase.login;


import com.blankj.utilcode.util.*;
import com.hainatong.jcmvvmbase.BR;
import com.hainatong.jcmvvmbase.Constants;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.ActivityLoginBinding;
import com.hainatong.jcmvvmbase.login.viewmodel.LoginViewModel;
import com.hainatong.jcmvvmbase.mainhome.MainHomeActivity;
import com.hainatong.jcmvvmbase.utils.ClickUtil;
import com.hainatong.jcmvvmbase.utils.GsonUtil;
import com.hainatong.jcmvvmbase.widget.LoadingDialog;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;


public class LoginActivity extends BaseMvvmActivity<ActivityLoginBinding, LoginViewModel>
{
    private LoadingDialog loadingDialog;

    private boolean isOpenPwd = false;

    public static void start(Context context)
    {
        Intent start = new Intent(context, LoginActivity.class);
        context.startActivity(start);
        ((Activity)context).overridePendingTransition(android.R.anim.fade_in,
            android.R.anim.fade_out);
    }

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_login;
    }

    @Override
    protected int getBindingVariable()
    {
        return BR.loginviewmodel;
    }

    @Override
    protected void initView()
    {
        loadingDialog = new LoadingDialog(this, getString(R.string.loading));

        viewDataBinding.btnLogin.setOnClickListener(view -> {
            if (ClickUtil.isFastClick(500, view))
            {
                return;
            }

            if (!StringUtils.isEmpty(viewModel.username.get()))
            {
                // if(!Validator.isMobile( viewModel.username.get() ))
                if (false)
                {
                    ToastUtils.showShort(getString(R.string.error_phone_tips));
                    return;
                }
            }
            else
            {
                ToastUtils.showShort(getString(R.string.input_phone));
                return;
            }
            if (!StringUtils.isEmpty(viewModel.pwd.get()))
            {
                // if( !Validator.isPassword( viewModel.pwd.get() ) )
                if (false)
                {
                    ToastUtils.showShort(getString(R.string.error_pwd_tips));
                    return;
                }
            }
            else
            {
                ToastUtils.showShort(getString(R.string.input_pwd));
                return;
            }
            loadingDialog.show();
            viewModel.login().observe(LoginActivity.this, loginResult -> {

                loadingDialog.hide();

                if (loginResult.isLoginSuccess())
                {
                    String userJson = GsonUtil.obj2json( loginResult.getUser() );
                    LogUtils.e( userJson );
                    SPUtils.getInstance(Constants.CONFIG).put(Constants.LOCAL_USER, userJson);

                    ToastUtils.showShort("登录成功");
                    MainHomeActivity.start(LoginActivity.this);
                }
                else
                {
                    ToastUtils.showShort("" + loginResult.getMsg());
                }
            });

            // startActivity( new Intent( this,ListActivity.class ) );

        });

        viewDataBinding.ivShowPwd.setOnClickListener(view -> {

            if (ClickUtil.isFastClick(500, view))
            {
                return;
            }
            isOpenPwd = !isOpenPwd;
            if (isOpenPwd)
            {
                // 明文
                viewDataBinding.etPassword.setTransformationMethod(
                    HideReturnsTransformationMethod.getInstance());
                viewDataBinding.ivShowPwd.setImageResource(R.mipmap.ic_eye_open);
            }
            else
            {
                // 显示密文
                viewDataBinding.etPassword.setTransformationMethod(
                    PasswordTransformationMethod.getInstance());
                viewDataBinding.ivShowPwd.setImageResource(R.mipmap.ic_eye_close);
            }
        });

        viewDataBinding.forgetPwd.setOnClickListener(v -> {
            ToastUtils.showShort(getString(R.string.call_manager));
        });
    }

    @Override
    protected void initData()
    {

    }

    @Override
    protected void onRetryBtnClick()
    {

    }
}
